const strTime = (format,formatObj) => format.replace(/{(y|m|d|h|i|s)+}/g, (result, key) => {
	let value = formatObj[key]
	if (result.length > 0 && value < 10) {
		value = '0' + value
	}
	return value
})
const formatDateYMDHM = (date) => `${date.getFullYear()}-${(date.getMonth() + 1)<10?'0'+(date.getMonth() + 1):date.getMonth() + 1}-${(date.getDate())<10?'0'+(date.getDate()):date.getDate()} ${(date.getHours())<10?'0'+(date.getHours()):date.getHours()}:${(date.getMinutes())<10?'0'+(date.getMinutes()):date.getMinutes()}`;
const parseTimeAll = (time) => {
	const format = '{y}-{m}-{d} {h}:{i}:{s}'
	let date = null
	if (typeof time === 'string') {
		time = parseInt(time)
	}
	date = new Date(time*1000)
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds()
	}
	
	return strTime(format,formatObj)
}
const parseTimeAllYMDHM = (time) => {
	const format = '{y}-{m}-{d} {h}:{i}'
	let date = null
	if (typeof time === 'string') {
		time = parseInt(time)
	}
	date = new Date(time*1000)
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes()
	}
	
	return strTime(format,formatObj)
}
const parseTimeMD = (time) => {
	const format = '{m}月{d}日'
	const formatDate = '{y}年{m}月{d}日'
	let date1 = null
	if (typeof time === 'string') {
		time = parseInt(time)
	}
	date1 = new Date(time*1000)
	const formatObj1 = {
		y: date1.getFullYear(),
		m: date1.getMonth() + 1,
		d: date1.getDate()
	}
	let date2 = new Date()
	const formatObj2 = {
		y: date2.getFullYear(),
		m: date2.getMonth() + 1,
		d: date2.getDate()
	}
	let compare1 = strTime(formatDate,formatObj1)
	let compare2 = strTime(formatDate,formatObj2)
	if(compare1==compare2){
		return '今天'
	}else{
		return strTime(format,formatObj1)
	}
}
/** 
 * file鏉炵悿ase64
*/
const blobToDataURL = (blob, cb) => {
    let reader = new FileReader();
    reader.onload = function (evt) {
        let base64 = evt.target.result;
        cb(base64);
    };
    reader.readAsDataURL(blob);
};
const hasNull = (params) => {
	var keys = Object.keys(params);
	for (var i = 0; i < keys.length; i++) {
		var obj = params[keys[i]];
		// console.log(keys[i],obj,obj == "")
		if(obj==0){
			return false;
		}
		if (obj == null || obj === "" || obj == undefined) {
			return true;
		}
	}
	return false;
}

const filterRole = (value) => {
	let item = {}
  if(value==1){
	   item = {text:'教师',bgc:'#CDD8FF',color:'#577BFD'}
  }else if(value==2){
	  item = {text:'学生',bgc:'#C5E2FF',color:'#3397FA'}
  }else if(value==3){
	  item = {text:'志愿者',bgc:'#FFC4B8',color:'#FC602F'}
  }
  return item
}

const filterCourseType = (value) => {
	let text=''
	if(value==1){
	   text = '历史学'
	}else if(value==2){
	  text = '文化生活'
	}else if(value==3){
	  text = '其他'
	}
	return text
}

const filterMessageType = (value) => {
	let text=''
	if(value==1){
	   text = '课程消息'
	}else if(value==2){
	  text = '系统消息'
	}
	return text
}


const filterCourseStatus = (value) => {
	let text=''
	if(value==1){
	   text = '报名中'
	}else if(value==2){
	  text = '已完成'
	}else if(value==3){
	  text = '进行中'
	}
	return text
}
const parseToDateAll = (val) => {
	const format = '{y}-{m}-{d} {h}:{i}:{s}'
	let date = new Date(val)
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds()
	}
	return strTime(format,formatObj)
}
const parseToDateYM = (val) => {
	const format = '{y}-{m}'
	let date = new Date(val)
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1
	}
	return strTime(format,formatObj)
}
export default{
	parseToDateYM,
	parseToDateAll,
    formatDateYMDHM,
    blobToDataURL,
    hasNull,
	filterRole,
	filterCourseType,
	filterCourseStatus,
	filterMessageType,
	parseTimeMD,
	parseTimeAll,
	parseTimeAllYMDHM
}